package com.lixiaoyi.ems.filter;


import com.lixiaoyi.ems.entity.Emp;
import com.lixiaoyi.ems.entity.EmpManager;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author 李小毅
 * @Description
 * @Date 2022-05-18
 */

@WebFilter("/manager/safe/*")
public class CheckFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        //强转类型
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        HttpSession session = request.getSession();
        //获取session中的empManager
        EmpManager empManager = (EmpManager) session.getAttribute("empManager");
        if (empManager != null){
            //如果非空 证明已经登陆  放行过滤
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            //空 重定向 登陆界面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        }

    }

    @Override
    public void destroy() {

    }
}
